package com.mkyong.client;
 
import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;

public class SOAPMsg{
 
	public static void main(String[] args) throws Exception {
 
		try {
		      
	         //First create the connection
	         SOAPConnectionFactory soapConnFactory = 
	                            SOAPConnectionFactory.newInstance();
	         SOAPConnection connection = 
	                             soapConnFactory.createConnection();
	         
	       //Next, create the actual message
	         MessageFactory messageFactory = MessageFactory.newInstance();
	         SOAPMessage message = messageFactory.createMessage();
	         
	         //Create objects for the message parts            
	         SOAPPart soapPart =     message.getSOAPPart();
	         SOAPEnvelope envelope = soapPart.getEnvelope();
	         SOAPBody body =         envelope.getBody();

	       //Populate the body
	         //Create the main element and namespace
	         SOAPElement bodyElement = 
	                   body.addChildElement(envelope.createName("GetVersionRequest", // Nome do método
	                                                                 "ns1", 			 // Tanto faz
	                                           		"http://www.ebay.com/marketplace/search/v1/services"));				 // targetNamespace
	         
	         //Add content
	         bodyElement.addChildElement("categoryId").addTextNode("1");

	         //Save the message
	         message.saveChanges();


	         //Check the input
	         System.out.println("\nREQUEST:\n");
	         message.writeTo(System.out);
	         System.out.println();
	         
	       //Send the message and get a reply   
	            
	         //Set the destination
	         String destination = "https://svcs.ebay.com/services/search/FindingService/v1";
	         //Send the message
	         SOAPMessage reply = connection.call(message, destination);
	         
	       //Check the output
	         System.out.println("\nRESPONSE:\n");
	         //Create the transformer
	         TransformerFactory transformerFactory = 
	                            TransformerFactory.newInstance();
	         Transformer transformer = 
	                         transformerFactory.newTransformer();
	         //Extract the content of the reply
	         Source sourceContent = reply.getSOAPPart().getContent();
	         //Set the output for the transformation
	         StreamResult result = new StreamResult(System.out);
	         transformer.transform(sourceContent, result);
	         System.out.println();

	         //Close the connection            
	         connection.close();
	            
	        } catch(Exception e) {
	            System.out.println(e.getMessage());
	        }
	}
 
}